<template>
  <div class="rr-login">
    <!-- 1、登录磨砂背景 -->
    <div class="rr-login-wrap-bg"></div>

    <!-- 2、登录主体 -->
    <div class="rr-login-wrap">
      <div class="rr-login-right">
        <div class="rr-login-right-main">
          <!-- 2.1、标题 -->
          <h4 class="rr-login-right-main-title">
            <span>{{ $t("ui.login.welcome") }}</span>
            <span>{{ $t("ui.app.productName") }}</span>
            <!-- <span class="rr-login-right-main-lang">
              <lang>
                <svg-icon name="fanyiline"></svg-icon>
              </lang>
            </span> -->
          </h4>

          <!-- 2.2、登录表单 -->
          <el-form ref="formRef" label-width="80px" :status-icon="true" :model="login" :rules="rules" @keyup.enter="onLogin">
            <el-form-item label-width="0" prop="username">
              <el-input v-model="login.username" :placeholder="$t('ui.login.userNamePlaceholder')" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label-width="0" prop="password">
              <el-input :placeholder="$t('ui.login.passwordPlaceholder')" v-model="login.password" autocomplete="off" show-password></el-input>
            </el-form-item>
            <!-- <el-form-item label-width="0" prop="captcha">
              <el-space class="rr-login-right-main-code">
                <el-input v-model="login.captcha" :placeholder="$t('ui.login.captchaPlaceholder')" prefix-icon="first-aid-kit"></el-input>
                <img style="vertical-align: middle; height: 40px; cursor: pointer" :src="state.captchaUrl" @click="onRefreshCode" alt="" />
              </el-space>
            </el-form-item> -->
            <el-form-item label-width="0">
              <el-button type="primary" size="small" :disabled="state.loading" @click="onLogin" class="rr-login-right-main-btn">
                {{ $t("ui.login.loginBtn") }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { CacheToken } from "@/constants/cacheKey";
import Lang from "@/components/base/lang/index";
import baseService from "@/service/baseService";
import { setCache } from "@/utils/cache";
import { ElMessage } from "element-plus";
// import { getUuid } from "@/utils/utils";
import app from "@/constants/app";
import SvgIcon from "@/components/base/svg-icon/index";
import { useAppStore } from "@/store";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import Crypoto from "@/utils/AES";

const store = useAppStore();
const router = useRouter();
const { t } = useI18n();

const cry: any = new Crypoto();

const state = reactive({
  // captchaUrl: "",
  loading: false,
  year: new Date().getFullYear()
});

// const login = reactive({ username: "", password: "", captcha: "", uuid: "" });
const login = reactive({ username: "", password: "" });

onMounted(() => {
  //清理数据
  store.logout();
  // getCaptchaUrl();
});
const formRef = ref();

const rules = ref({
  username: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  password: [{ required: true, message: t("validate.required"), trigger: "blur" }]
  // captcha: [{ required: true, message: t("validate.required"), trigger: "blur" }]
});

// const getCaptchaUrl = () => {
//   login.uuid = getUuid();
//   state.captchaUrl = `${app.api}/captcha?uuid=${login.uuid}`;
// };

// const onRefreshCode = () => {
//   getCaptchaUrl();
// };

const onLogin = () => {
  formRef.value.validate((valid: boolean) => {
    const pwd = cry.encrypt(login.password);
    if (valid) {
      state.loading = true;
      const params = {
        username: login.username,
        password: pwd
      };
      baseService
        .post("/login", params)
        .then((res) => {
          state.loading = false;
          if (res.code === 0) {
            res.data.password = pwd;
            res.data.unncryptPassword = login.password;
            console.log("res.data=", res.data);
            setCache(CacheToken, res.data, true);
            ElMessage.success(t("ui.login.loginOk"));
            router.push("/");
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {
          state.loading = false;
          // onRefreshCode();
        });
    }
  });
};
</script>

<style lang="less" scoped>
@import url("@/assets/theme/base.less");
.rr-login {
  width: 100vw;
  height: 100vh;
  background: url("../assets/images/login-bg.png") center no-repeat;
  -moz-background-size: 100% 100%;
  background-size: 100% 100%;
  position: relative;

  &-wrap {
    width: 38%;
    min-width: 500px;
    animation-duration: 1s;
    animation-fill-mode: both;
    border-radius: 5px;
    overflow: hidden;
    height: 100%;
    z-index: 1;
    background: #ffffff33;
  }
  &-wrap-bg {
    width: 40%;
    min-width: 500px;
    filter: blur(6px);
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: #ffffff44;
    z-index: 0;
  }

  &-right {
    border-left: none;
    color: #fff;
    height: 100%;
    min-height: 500px;
    position: relative;
    align-items: center;
    display: flex;
    &-main {
      margin: 0 auto;
      width: 70%;
      &-title {
        display: flex;
        flex-direction: column;
        line-height: 53px;
        font-family: MiSans-SNaNpxibold;
        font-weight: 600;
        font-size: 36px;
        color: #0b1526;
        margin-bottom: 36px;
      }

      &-lang .iconfont {
        font-size: 20px;
        color: #606266;
        font-weight: 800;
        width: 20px;
        height: 20px;
      }

      /deep/.el-input__wrapper {
        height: 58px;
        background: #ffffff66;
        box-shadow: 0 0 0 1px #ffffff99 inset;
        border-radius: 8px;
        font-size: 16px;
        padding: 18px 28px 18px 32px;
      }

      &-code {
        width: 100%;
        .el-space__item:first-child {
          flex: 1;
        }
      }
      &-btn:hover {
        background-color: #0069e9;
      }
      &-btn {
        width: 100%;
        height: 58px;
        background: #4d89ff;
        border-radius: 8px;
        transition: 0.25s;
        margin-top: 40px;
        font-family: PingFangSC-SNaNpxibold;
        font-weight: 600;
        font-size: 20px;
        color: #ffffff;
        letter-spacing: 2px;
      }
    }
  }

  @keyframes animate-down {
    0%,
    60%,
    75%,
    90%,
    to {
      animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
    }
    0% {
      opacity: 0;
      transform: translate3d(0, -3000px, 0);
    }
    60% {
      opacity: 1;
      transform: translate3d(0, 25px, 0);
    }
    75% {
      transform: translate3d(0, -10px, 0);
    }
    90% {
      transform: translate3d(0, 5px, 0);
    }
    to {
      transform: none;
    }
  }

  .animate-down {
    animation-name: animate-down;
  }
}
</style>
<style lang="scss">
input:-webkit-autofill {
  height: 0;
  padding: 1.2em 0.5em;
  background-clip: content-box;
  &::first-line {
    color: white !important;
  }
  -webkit-text-fill-color: #141311;
}
</style>
